package com.cz.nongyetongv5.activity;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.adapter.config.CommonConstants;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.custom.BlueLoadingProgress;
import com.cz.nongyetongv5.utils.ConfigUtil;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class NongYaoChaXunDetail extends Activity implements
		View.OnClickListener {

	private ViewGroup btnBack = null;

	private BlueLoadingProgress blpLoading = null;

	private WebView wvChaXun = null;

	private ViewGroup btnRefresh = null;

	private String pid;

	@Override
	protected void onDestroy() {

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().remove(this);

		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().add(this);

		setContentView(R.layout.nongyaochaxun_detail);

		pid = getIntent().getStringExtra("pid");

		wvChaXun = (WebView) findViewById(R.id.wvChaXun);

		// 自动加载图片
		wvChaXun.getSettings().setLoadsImagesAutomatically(true);

		wvChaXun.getSettings().setSupportZoom(true);
		wvChaXun.getSettings().setBuiltInZoomControls(true);

		// 启用脚本
		wvChaXun.getSettings().setJavaScriptEnabled(true);

		wvChaXun.setWebViewClient(new WebViewClient() {

			@Override
			public void onPageFinished(WebView view, String url) {

				if (blpLoading != null) {

					blpLoading.stopLoading();

					blpLoading.setVisibility(View.GONE);
				}

			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);

				if (blpLoading != null) {
					blpLoading.setVisibility(View.VISIBLE);
					blpLoading.startLoading();

				}
			}

		});

		blpLoading = (BlueLoadingProgress) findViewById(R.id.blpLoading);
		blpLoading.setVisibility(View.GONE);

		btnBack = (ViewGroup) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(this);

		btnRefresh = (ViewGroup) findViewById(R.id.btnRefresh);

		btnRefresh.setOnClickListener(this);

		String url = new ConfigUtil(getResources()).getPreHttpUrl()
				+ getString(R.string.nongyaochaxun_biaoqain) + pid + ".html";

		url = url.replace("index.php/", "");

		wvChaXun.loadUrl(url);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {

		if (v == btnBack) {

			finish();
		}

		if (btnRefresh == v) {

			if (wvChaXun != null) {

				wvChaXun.reload();
			}

		}

	}

}
